package com.example.houseitemmanagebackend.controller;

import com.example.houseitemmanagebackend.dto.MessageDTO;
import com.example.houseitemmanagebackend.service.MessageService;
import com.example.houseitemmanagebackend.vo.MessageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息控制器
 */
@RestController
@RequestMapping("/messages")
@Tag(name = "消息管理", description = "消息和活动记录相关接口")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 获取用户消息列表
     */
    @GetMapping
    @Operation(summary = "获取消息列表", description = "获取当前位置的通知和活动记录")
    public ResponseEntity<Map<String, Object>> getMessages(
            @Parameter(description = "位置ID，筛选指定位置的消息") @RequestParam(required = false) Integer locationId) {

        // 安全检查：确保用户只能查看自己的消息
        // 在实际应用中，通常会从安全上下文中获取当前用户ID，而不是从参数中传入

        List<MessageVO> messageList = messageService.getMessages(locationId);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "成功");
        result.put("data", messageList);

        return ResponseEntity.ok(result);
    }

    /**
     * 标记单个消息为已读
     */
    @PutMapping("/{messageId}/read")
    @Operation(summary = "标记消息为已读", description = "将指定ID的消息标记为已读状态")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<Map<String, Object>> markAsRead(
            @Parameter(description = "消息ID") @PathVariable Long messageId) {

        boolean success = messageService.markAsRead(messageId);

        Map<String, Object> result = new HashMap<>();
        result.put("code", success ? 200 : 400);
        result.put("message", success ? "标记成功" : "标记失败");

        return ResponseEntity.ok(result);
    }

    /**
     * 标记所有消息为已读
     */
    @PutMapping("/read-all")
    @Operation(summary = "标记所有消息为已读", description = "将当前位置的所有未读消息标记为已读")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<Map<String, Object>> markAllAsRead(
            @Parameter(description = "位置ID") @RequestParam Integer locationId) {

        messageService.markAllAsReadByLocation(locationId);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "标记成功");

        return ResponseEntity.ok(result);
    }

    /**
     * 创建新消息
     */
    @PostMapping
    @Operation(summary = "创建新消息", description = "创建新的通知或活动记录")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<Map<String, Object>> createMessage(
            @Valid @RequestBody MessageDTO messageDTO) {

        // 安全检查：确保用户只能为自己或有权限的用户创建消息
        // 在实际应用中，应该验证当前用户是否有权限为指定用户创建消息

        Integer messageId = messageService.createMessage(messageDTO);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "创建成功");

        Map<String, Object> data = new HashMap<>();
        data.put("id", messageId);

        result.put("data", data);

        return ResponseEntity.ok(result);
    }
}